<?php

namespace App\Kuafu\Shop\Controller\Pos;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Service\PrinterService;
use App\Kuafu\Shop\Validate\PrinterValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PrinterController extends BaseController
{
    /**
     * 根据id获取打印机详情
     * @param Request $request
     * @return JsonResponse
     */
    public function getPrinterDetail(Request $request)
    {
        $id = (int)$request->id;
        $valid = PrinterValidate::printerById(['id' => $id]);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }
        $info = PrinterService::getPrinterById($id);

        return $this->success($info);
    }

    /**
     * 打印机列表
     * @param Request $request
     * @return JsonResponse
     */
    public function getPrinterList(Request $request)
    {
        return $this->success(PrinterService::getPrinterList($request->header('shopId'), $request->header('brandId')));
    }

    /**
     * 新增打印机
     * @param Request $request
     * @return JsonResponse
     * @throws ShopException
     */
    public function addPrinterData(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['shopId'] = $request->header('shopId');
        $data['brandId'] = $request->header('brandId');
        $valid = PrinterValidate::addPrinter($data);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }

        $printer = PrinterService::addPrinter($request->name, $request->header('shopId'), $request->header('brandId'), $request->printerType, $request->deviceType, $request->printerIp, $request->status, $request->deviceId, $request->printerPort);
        return $this->success($printer);
    }

    /**
     * 删除打印机（软删除）
     * @param Request $request
     * @return JsonResponse
     * @throws ShopException
     */
    public function delPrinter(Request $request)
    {
        $id = (int)$request->id;
        $valid = PrinterValidate::printerById(['id' => $id]);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }
        return $this->success(PrinterService::delPrinter($id));
    }

    /**
     * 根据id修改打印机信息
     * @param Request $request
     * @return JsonResponse
     * @throws ShopException
     */
    public function setPrinterData(Request $request)
    {
        $data = $request->all();
        $data['shopId'] = $request->header('shopId');
        $data['brandId'] = $request->header('brandId');
        $valid = PrinterValidate::setPrinter($data);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_SHOP_INPUT, $valid);
        }

        $printer = PrinterService::setPrinter($request->id, $request->name,  $request->printerType, $request->deviceType, $request->printerIp, $request->printerPort, $request->status, $data['shopId'], $data['brandId']);
        return $this->success($printer->toArray());
    }
}
